<!--
* description: 总部移库指令发起---新增
* author: duanhy
* createdDate: 2020-11-11
-->
<template>
  <el-dialog
    v-dialogDrag
    class="dialog"
    :visible="isDialogVisible"
    width="70%"
    :append-to-body="true"
    :close-on-click-modal="false"
    @close="closeEdit"
  >
    <dialogHeader slot="title" :title="titleName" :style-type="$store.state.app.dialogType" />
    <div
      v-loading="loading"
    >
      <one-table-template
        ref="multipleTable"
        :dynamic-table-defult-height="380"
        :dynamic-api-config="apiConfig"
        :dynamic-buttons="dynamicButtons"
        :dynamic-components="tableComponents"
        :dynamic-form-fields="formField"
        :refresh-table-key="refreshTableKey"
      />
    </div>
  </el-dialog>
</template>

<script>
import dialogHeader from '@/components/dialogHeader'
import OneTableTemplate from '@/components/templates/oneTable'
import apiTransfer from '@/api/apiVeTransferManagement'
export default {
  components: {
    dialogHeader,
    OneTableTemplate
  },
  props: {
    titleName: {
      type: String,
      default: ''
    },
    isDialogVisible: {
      type: Boolean,
      default: false
    },
    dynamicEditRowData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path
    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])
    return {
      editRowData: {},
      loading: false,
      refreshTableKey: '0',
      // table表格查询API配置对象
      apiConfig: {},
      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableComponents
          : [
            {
              compKey: 'compKey1',
              labelName: '源仓库：',
              codeField: 'oldHouseName',
              textField: 'oldHouseId',
              isRequire: true,
              dataToObject: {
                title: '源仓库选择',
                warehouseId: '3'
              },
              component: () => import('./WarehouseSelect/index'),
              isMust: true,
              span: 8
            },
            {
              compKey: 'compKey2',
              labelName: '源仓库地址：',
              codeField: 'oldHouseAddr',
              disabled: true,
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              span: 8
            },
            {
              compKey: 'compKey3',
              labelName: 'VIN：',
              codeField: 'vin',
              textField: 'vinlist',
              isRequire: true,
              disabled: true,
              component: () => import('./VINSelect/index'),
              dataToObject: {
                storeOrZb: true
              },
              type: 'dropdownList',
              isMust: true,
              span: 8
            },
            {
              compKey: 'compKey4',
              labelName: '目的库：',
              codeField: 'stockName',
              textField: 'stockId',
              isRequire: true,
              dataToObject: {
                title: '目的库选择',
                warehouseId: '1'
              },
              component: () => import('./WarehouseSelect/index'),
              isMust: true,
              span: 8
            },
            {
              compKey: 'compKey5',
              labelName: '目的库地址：',
              codeField: 'stockAddress',
              disabled: true,
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              span: 8
            }
          ],
      // form 的表单对象
      formField: {
        vin: '',
        vinlist: {},
        stockName: '',
        stockId: '',
        stockAddress: '',
        oldHouseId: '',
        oldHouseName: '',
        oldHouseAddr: ''
      },

      // 动态组件-按钮
      dynamicButtons:
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableButtons &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableButtons
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableButtons
          : [
            {
              compKey: 'btnKey1',
              type: 'primary',
              size: 'small',
              icon: 'iconfont  icon-preservation',
              clickEvent: () => this.save(),
              text: '确认',
              position: 'right'
            },
            {
              compKey: 'btnKey2',
              type: '',
              size: 'small',
              icon: 'iconfont  icon-cancel',
              clickEvent: () => this.closeEdit(),
              text: '取消',
              position: 'right'
            }
          ]
    }
  },
  watch: {
    isDialogVisible: {
      handler: function(val) {
        if (this.titleName !== '总部移库指令新增') {
          this.tableComponents[0].dataToObject.warehouseId = '5'
          this.tableComponents[2].dataToObject.storeOrZb = 'store'
        } else {
          this.tableComponents[0].dataToObject.warehouseId = '3'
          this.tableComponents[3].dataToObject.warehouseId = '4'
          this.tableComponents[2].dataToObject.storeOrZb = 'zb'
        }
      },
      immediate: true
    },
    'formField.oldHouseId': {
      handler(oldHouseId) {
        if (oldHouseId !== '') {
          this.tableComponents[2].disabled = false
        } else {
          this.tableComponents[2].disabled = true
          this.formField.vin = ''
          this.formField.oldHouseAddr = ''
          this.formField.vinlist = {}
        }
      },
      deep: true
    },
    'formField.stockId': {
      handler(stockId) {
        if (stockId === '') {
          this.formField.stockAddress = ''
        }
      },
      deep: true
    }
  },
  methods: {
    // 关闭弹窗
    closeEdit(type) {
      this.$emit('close', type)
    },
    VINClose() {
      this.VINShow = false
    },
    // 保存
    save() {
      this.$utils.validataMoth(this, 'validpage')
      if (this.valid) {
        this.loading = true
        const lists = [{ ...this.formField, ...this.formField.vinlist }]
        delete lists[0].vinlist
        var addApi
        if (this.titleName === '总部移库指令新增') {
          addApi = apiTransfer.insertRelocation
        } else {
          addApi = apiTransfer.insertRelocationStore
        }
        this.$requestAPI(this.$getParams({
          apiConfig: addApi,
          variables: {
            vinSubmit: '1',
            list: lists }
        })).then(response => {
          this.loading = false
          if (response.result === '0') {
            if (response.msg) {
              this.$message({ message: response.msg, type: 'warn', uration: 2000 })
            }
            return
          }
          this.$message({ message: response.msg, type: 'warn', uration: 2000 })
          setTimeout(() => {
            this.closeEdit(1)
          }, 500)
        }).catch(err => {
          console.info(err)
        })
      }
    }
  }
}
</script>

